'use strict';

import React, {Component} from 'react';
import {
	DateTime,
	Http,
	ArrayUtils
} from '../lib';
import {
	API,
	IMG,
	OPTION
} from '../res';

export default class extends Component {
	/*
	 * 构造函数。
	 * @param props 只读属性
	 */
	constructor(props) {
		super(props);
		this.base = {
			pageSize: 10,
			currentPage: 1,
			filter: {},
			checked: [],
			fileTypes: 'doc|docx|xls|xlsx|ppt|pptx|png|jpg|pdf|bmp|zip|rar'
		};
	}

	/*
	 * 全不选多选框。
	 * @param count 总数
	 */
	selectNone(count) {
		for (let i = 0; i < count; i++) {
			this.refs['checkbox' + i].refs.field.getFieldDOMNode().checked = false;
		}

		this.refs.checkboxAll.refs.field.getFieldDOMNode().checked = false;
		this.base.checked = [];

		this.setState({
			keyOperationButtonDisabled: true
		});
	}

	/*
	 * 全选多选框。
	 * 当多选框被禁用时不选。
	 * @param count 总数
	 */
	selectAll(count) {
		for (let i = 0; i < count; i++) {
			let checkbox = this.refs['checkbox' + i].refs.field.getFieldDOMNode();
			if (!checkbox.disabled) {
				checkbox.checked = true;
			}
		}
	}

	/*
	 * 处理多选框改变。
	 * @param index 索引值
	 * @param data 数据
	 */
	handleCheckBoxChange(index, data) {
		let status = this.refs['checkbox' + index].refs.field.getChecked();
		
		if (status) {
			this.base.checked.push(data);
		} else {
			this.refs.checkboxAll.refs.field.getFieldDOMNode().checked = false;
			ArrayUtils.removeByValue(this.base.checked, data);
		}

		let keyOperationButtonDisabled = true;

		if (this.base.checked.length !== 0) {
			keyOperationButtonDisabled = false;
		} else {
			keyOperationButtonDisabled = true;
		}
		this.setState({
			keyOperationButtonDisabled: keyOperationButtonDisabled,
		});
	}

	/*
	 * 处理全选多选框改变。
	 * @param datas 数据
	 */
	handleCheckBoxAllChange(datas) {
		let status = this.refs.checkboxAll.refs.field.getChecked();
		if (status) {
			this.selectAll(datas.length);
			for (let i = 0; i < datas.length; i++) {
				let checkbox = this.refs['checkbox' + i].refs.field.getFieldDOMNode();
				if (!checkbox.disabled) {
					this.base.checked.push(datas[i]);
				}
			}
		} else {
			this.selectNone(datas.length);
			this.base.checked = [];
		}


		let keyOperationButtonDisabled = true;
		if (this.base.checked.length !== 0) {
			keyOperationButtonDisabled = false;
		} else {
			keyOperationButtonDisabled = true;
		}
		this.setState({
			keyOperationButtonDisabled: keyOperationButtonDisabled,
		});
	}

	/*
	 * 查询请求。
	 * @param url 路由
	 * @param param 参数
	 * @param callback 回调
	 */
	query(url, param = {}, callback) {
		if (callback === undefined) {
			if (url === API.singlePlanProcess) {
				callback = data => {
					this.refs.processModal.show();

					this.setState({
						process: data.data
					});
				}
			} else {
				callback = data => {
					this.setState({
						datas: data.data.list,
						total: data.data.total,
					});
				};
			}
		}

		if (url === undefined) {
			console.log('function query: url is necessary.');
		} else {
			param.page = this.base.currentPage;
			param.size = this.base.pageSize;

			const filter = this.base.filter;
			for (let x in filter) {
				param[x] = filter[x];
			}

			Http.post(url, param, callback);
		}
	}

	/*
	 * 获取附件列表。
	 * @param value 附件
	 */
	getAttachList(value) {
		value = value !== undefined ? value.split(',') : [];
		value = value.map((x, i) => {
			return (
				<li key={i}>
					<a
						href={API.uploads + x}
						className="am-block"
					>{x}</a>
				</li>
			);
		});

		return <ul>{value}</ul>;
	}

	/*
	 * 获取进度列表。
	 * @param data 进度数据
	 */
	getProcessList(data) {
		const imgStyle = {
			width: 30,
			height: 40
		};

		return data.map((x, i) => {
			return (
				<div key={i}>
					<img 
						src={IMG.point} 
						alt="img" 
						style={imgStyle}
					/>
					<span className="am-margin-left">{x.workName}</span>
					<span className="am-margin-left text-gray">{DateTime.format(x.submitTime)}</span>
					<span className="am-margin-left text-gray">{x.actionPersonName}</span>
				</div>
			);
		});
	}

	/**
	 * 导出Excel。
	 *
	 * @param {String} url 地址
	 * @param {Number} type 导出类型
	 */
	exportExcel(url, type = 0) {
		let ids = this.state.ids;
		url = ids === undefined ? url : `${url}?all=${ids}&type=${type}`;
		window.location.href = url;
	}
	/**
	 * 整改统计导出Excel。
	 *
	 * @param {String} url 地址
	 * @param {Number} type 导出类型
	 */
	specialExportExcel(url, type = 0,group) {
		let ids = this.state.ids;
		url = ids === undefined ? url : `${url}?all=${ids}&type=${type}&group=${group}`;
		window.location.href = url;
	}

	/*
	 * 通过id获取性别。
	 * @param id 性别id
	 */
	getSexById(id) {
		const SEX = ['未设置','女','男'];
		return SEX[id];
	}

	/*
	 * 处理点击新增。
	 * @param e 事件
	 */
	handleClickAdd(e) {
		this.refs.addModal.show();
		this.setState({
			data: {}
		});
	}

	/*
	 * 处理点击关键操作按钮。
	 * @param modalRef 确认框名称
	 * @param idName 编号名称
	 */
	handleClickKeyOperationButton(modalRef, idName) {
		this.refs[modalRef].show();
		console.log( this.base)
		let ids = this.base.checked.map((x, i) => {
			return x[idName];
		});
		this.setState({
			ids: ids.toString(),
		});
	}

	/*
	 * 处理点击关键操作确定。
	 * @param idName 编号名称
	 * @param deleteURL 删除地址
	 * @param queryURL 查询地址
	 */
	handleClickOkOfKeyOperation(idName, deleteURL, queryURL) {
		let param = {};
		param[idName] = this.state.ids;
		console.log(param)
		Http.post(deleteURL, param, data => {
			this.query(queryURL);
			this.selectNone(this.state.datas.length);
		});
	}

	/*
	 * 处理点击导出所有。
	 * @param e 事件
	 */
	handleClickExportAll(e) {
		this.refs.exportAllModal.show();
		this.setState({
			ids: '',
		});
	}

	/**
	 * 处理点击导出确定。
	 *
	 * @param {String} url 导出请求地址
	 * @param {Number} type 导出类型
	 */
	handleClickOkOfExport(url, type = 0) {
		this.exportExcel(url, type);
		this.selectNone(this.state.datas.length);
	}
	/*
	 * 整改统计导出
	 * 
	 */
	handleClickOkOfSpecialExport(url, type = 0,group) {
		this.specialExportExcel(url, type,group);
	}
	/*
	 * 处理改变筛选。
	 * @param key 筛选项
	 * @param url 查询地址
	 * @param tableType 区分表格类型
	 * @param termType 区分表格头部
	 */
	handleChangeFilter(key, value, url) {
		this.base.filter[key] = value;
		 for(let i in this.base.filter){
		 	this.setState({
	 		termType: this.base.filter[i]
		 	})
		 	if(this.base.filter[i]==1||this.base.filter[i]==2){
		 	this.setState({
 			tableType:1
	 		})
		 	} else {
 				this.setState({
	 			tableType:0
	 			})
		 	}
		 }
		this.query(url);
	}
	/*
	 * 处理点击搜索输入框列表项。
	 * @param data 数据
	 */
	handleClickItemOfSearchInput(data) {
		this.setState({
			data: data,
		});
	}

	/*
	 * 处理点击列表项按钮。
	 * @param modalRef
	 * @param data
	 */
	handleClickTableItemButton(modalRef, data) {
		console.log(this.refs[modalRef])
		
		this.refs[modalRef].show();
		this.setState({data});
	}

	/*
	 * 处理点击建议保存。
	 * @param {String} idName 编号名称
	 * @param {String} adviceName 建议名称
	 * @param {String} adviceUrl 建议请求地址
	 * @param {String} queryUrl 查询请求地址
	 */
	handleClickSaveOfAdvice(idName, adviceName, adviceUrl, queryUrl) {
		let adviceText = this.refs.adviceText;
		if (!adviceText.validate()) {
			alert('请填写建议。');
		} else {
			let param = {};
			param[idName] = this.state.data[idName];
			param[adviceName] = adviceText.getValue();
			Http.post(adviceUrl, param, data => {
				this.refs.adviceModal.close();
				this.query(queryUrl);
			});
		}
	}

	/*
	 * 处理点击编辑保存。
	 * @param e 事件
	 */
	handleClickSaveOfEdit(e) {
		const avatar = this.refs.avatar;
		const realname = this.refs.realname;
		const sex = this.refs.sex;
		const idcard = this.refs.idcard;
		const mobile = this.refs.mobile;
		const unit = this.refs.unit;
		const education = this.refs.education;
		const job = this.refs.job;
		const major = this.refs.major;
		const political = this.refs.political;
		const unitWithJob = this.refs.unitWithJob;
		const resume = this.refs.resume;
		const param = {
			userid: this.state.data.userid,
			avatar: avatar.getBase64(),
			realname: realname.getValue(),
			sex: sex.getValue(),
			idcard: idcard.getValue(),
			mobile: mobile.getValue(),
			unitId: unit.getValue(),
			education: education.getValue(),
			job: job.getValue(),
			major: major.getValue(),
			political: political.getValue(),
			unitWithJob: unitWithJob.getValue(),
			resume: resume.getValue()
		};
		Http.post(API.memberUpdate, param, data => {
			this.refs.editModal.close();
			this.query();
		});
	}
	
	/*
	 * 处理点击督学个人工作计划。
	 * @param e 事件
	 */
	handleClickSinglePlan(e) {
		e.preventDefault();
		this.refs.singlePlanModal.show();
		this.setState({
			singlePlanData: {}
		});
		this.query(API.singlePlanIndex, {
			singlePlanId: this.state.data.planId
		}, (data) => {
			this.setState({
				singlePlanData: data.data.list[0]
			});
		});
	}

	/*
	 * 处理点击整改通知。
	 * @param e 事件
	 */
	handleClickNotice(e) {
		e.preventDefault();
		this.refs.noticeModal.show();
		this.setState({
			noticeData: {}
		});
		this.query(API.rectifyInformIndex, {
			rectifyInformId: this.state.data.rectifyInformId
		}, (data) => {
			this.setState({
				noticeData: data.data.list[0]
			});
		});
	}

	/**
	 * 字符串转换为选项。
	 * @param {String} value
	 * @param {String} label
	 * @return {Array}
	 */
	string2options(value = '', label = '') {
		value = value.split(',');
		label = label.split(',');
		let options = [];

		for (let i = 0; i < value.length; i++) {
			options.push({
				value: value[i],
				label: label[i]
			});
		}

		return options;
	}

	/**
	 * 通过id获得分类名称。
	 * @param {String} id
	 * @return {String}
	 */
	getClassifyById(id) {
		let name = '';

		for (let i = 0; i < OPTION.classify.length; i++) {
			if (OPTION.classify[i].value === id) {
				name = OPTION.classify[i].label;
			}
		}

		return name;
	}
}
